public class MySingleList implements IList {
    //结点内部类
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //链表的第一个结点
    public ListNode head;

    //头插
    @Override
    public void addFirst(int data) {
        //实例化一个结点对象
        ListNode newNode = new ListNode(data);
        //链表为空
        if(this.head == null) {
            this.head = newNode;
            return;
        }
        //链表不为空
        newNode.next = head;
        head = newNode;
    }

    //尾插
    @Override
    public void addLast(int data) {
        //实例化一个结点对象
        ListNode newNode = new ListNode(data);
        //链表为空
        if(this.head == null) {
            this.head = newNode;
            return;
        }
        //链表不为空
        ListNode cur = this.head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
    }

    //指定位置插入,假设第一个结点标记为0位置
    @Override
    public void addIndex(int index, int data) {
        //非法下标，抛出自定义异常
        if(index < 0 || index > size()) {
            throw new IllegalIndexException("Illegal Index !:下标非法");
        }
        //插入位置为0，相当于头插
        if(index == 0) {
            addFirst(data);
            return;
        }
        //寻找指定下标的前一个结点，方便更改链表的指向
        ListNode newNode = new ListNode(data);
        ListNode cur = this.head;
        for(int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        newNode.next = cur.next;
        cur.next = newNode;
    }

    //检查是否包含某个元素
    @Override
    public boolean contains(int key) {
        if(this.head == null) {
            return false;
        }
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一个指定的数据
    @Override
    public void remove(int key) {
        if(this.head == null) {
            throw new ListIsEmptyException("The SingleList is Empty!: 链表为空！");
        }
        //第一个结点满足
        if(head.val == key) {
            head = head.next;
            return;
        }
        //遍历寻找并删除
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }
    }

    //删除全部的指定数据
    @Override
    public void removeAllKey(int key) {
        if(this.head == null) {
            throw new ListIsEmptyException("The SingleList is Empty!: 链表为空！");
        }
        //先删除所有第一个结点后面的指定结点
        ListNode prev = this.head;
        ListNode cur = prev.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }
        //最后检查第一个结点是否满足删除条件
        if(this.head.val == key) {
            head = head.next;
        }
    }

    //链表结点数
    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清空
    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    //打印链表元素
    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}

class TestSingleList {
    public static void main(String[] args) {
        MySingleList singleList = new MySingleList();
//        singleList.addLast(1);
//        singleList.addLast(2);
////        singleList.addFirst(3);
////        singleList.addFirst(4);
//        singleList.addIndex(1, 100);
        singleList.addLast(3);
        singleList.addLast(1);
        singleList.addLast(1);
        singleList.addLast(1);
        singleList.addLast(2);
        singleList.remove(10);
        singleList.display();
        System.out.println(singleList.size());

    }
}
